<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // [1, 2, 3, 4, 5].forEach((item, index) => {
    //   console.log(`index=${index},item=${item},this=${this}`);
    // }, [4, 5, 6])
    [1, 2, 3, 4, 5].forEach(function (item, index) {
      // console.log(a, this)
      console.log(`index=${index},item=${item},this=${this}`);
    }, [6, 7, 8, 9])
    // Array.prototype.forEach2 = function (callback, thisArg) {
    //   if (this === null) {
    //     throw new TypeError('this is null')
    //   }
    //   if (typeof callback !== 'function') {
    //     throw new TypeError('callback is not a function')
    //   }
    //   for (let i = 0; i < this.length; i++) {
    //     callback.call(thisArg, this[i], i, this)
    //   }
    // }
    Array.prototype.forEach2 = function (callback, thisArg) {
      if (this === null) {
        throw new Error('this is null')
      }
      if (typeof callback !== 'function') {
        throw new Error('callbcak must be a function')
      }
      for (let i = 0; i < this.length; i++) {
        callback.call(thisArg, this[i], i, this)
      }
    }
    let arr = [1, 2, 3, 4, 5]
    arr.forEach2(function (item, index) { // 不能直接使用数组 不知道为啥
      // console.log(a, this)
      console.log(`index=${index},item=${item},this=${this}`);
    }, [6, 7, 8, 9])
  </script>
</body>

</html>